package org.jeecg.modules.cascv.image.job;


import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.jeecg.modules.message.websocket.WebSocket;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;

@Slf4j
@Service
public class GmvVideoJob {

    @Resource
    private WebSocket webSocket;

    @Value("${image.dirPath}")
    private String imageDirPath;

    public static int count = 0;

//    @Scheduled(cron = "0 0/5 * * * ? ")
//    @Scheduled(fixedRate = 30000)
    public void exec() {
        try {
            File file = new File(imageDirPath + File.separator + "json.json");
//            File file = new File("D:\\pc_workspace\\json.json");
            String arrayStr = FileUtils.readFileToString(file, "UTF-8");
            JSONArray jsonArray = JSONArray.parseArray(arrayStr);
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                log.info("请求图片合成接口！");
                HttpRequest request = HttpUtil.createPost("http://127.0.0.1:7810/jeecg-boot/test/gmvVideo/compositeImg");
                request.header("X-Access-Token", "qwertyuiplkjhgfdsazxcvbnmQWERTYUPLKJHGFDSAZXCVBNM1234567890");
                request.body(JSONObject.toJSONString(jsonObject));
                HttpResponse response = request.execute();
//                log.info(response.body());
            }
        } catch (Exception e) {
            log.error("添加视屏图片帧失败", e);
        }
    }

//    private String getImage() {
//        File file = new File(imageDirPath);
//        File[] files = file.listFiles();
//        if (files == null) return imageDirPath + File.separator + "1.jpg";
//        File image = files[count % files.length];
//        count++;
//        return image.getAbsolutePath();
//    }
}
